package com.heima.wemedia.controller.v1;

import cn.hutool.core.util.ObjectUtil;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.CustomException;
import com.heima.model.wemedia.dtos.WmDownOrUpDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Resource
    private WmNewsService wmNewsService;

    /**
     * 上/下架文章。
     * @param wmNewsDto
     * @return
     */
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmDownOrUpDto wmNewsDto){
        if(ObjectUtil.isEmpty(wmNewsDto.getId())){
//            501  文章id 不可缺少。
            return ResponseResult.errorResult(501 ,"文章id不可缺少");
        }
        WmNews wmNews = this.wmNewsService.getById(wmNewsDto.getId());
        if(ObjectUtil.isEmpty(wmNews)){
            return ResponseResult.errorResult(1002,"文章不存在");
        }
        if(wmNews.getStatus() != 9){
            return ResponseResult.errorResult(501,"文章不是发布状态，不能上下架");
        }
        wmNews.setEnable(wmNewsDto.getEnable());
        this.wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(200,"操作成功");
    }


    /**
     * 查看文章详情。
     * @param id
     * @return
     */

    @GetMapping("/one/{id}")
    public ResponseResult getById(@PathVariable("id")Integer id){
//        查看详情。
        WmNews wmNews = this.wmNewsService.getById(id);
        if(ObjectUtil.isEmpty(wmNews)){
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(wmNews);
    }

    /**
     * 根据文章id 删除 文章。news
     * @param id
     * @return
     */
    @GetMapping("/del_news/{id}")
    public ResponseResult deleteById(@PathVariable("id")Integer id){
        if(ObjectUtil.isEmpty(id)){
//            501  文章id 不可缺少。
            return ResponseResult.errorResult(501 ,"文章id不可缺少");
        }
        WmNews wmNews = this.wmNewsService.getById(id);
        if(ObjectUtil.isEmpty(wmNews)){
            return ResponseResult.errorResult(1002,"文章不存在");
        }
        if(wmNews.getStatus() == 9){
            return ResponseResult.errorResult(501,"文章已经发布，不能删除");
        }
        this.wmNewsService.removeById(id);
        return ResponseResult.okResult(200,"操作成功");

    }

    @PostMapping("/list")
    public ResponseResult<List<WmNews>> findAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }


    @PostMapping("/submit")
    public ResponseResult<Object> submitNews(@RequestBody WmNewsDto dto) {
        if(ObjectUtil.isEmpty(dto)){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wmNewsService.submitNews(dto);
    }

}
